//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Data;
using System.Configuration;
using EntLibContrib.Query.Parameters;

namespace EntLibContrib.Query.Configuration
{
    using System.Collections.Generic;
    using Database.Properties;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;

    /// <summary>
	/// Type to hold the elements of a single query parameter
	/// </summary>
    [ResourceDescription(typeof(DesignResources), "DataSetParameterDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "DataSetParameterDataDisplayName")]
    public class DataSetParameterData : DataParameterData
	{
		private const string columnProperty = "column";
		private const string versionProperty = "version";

		/// <summary>
		/// Gets or sets the source column.
		/// </summary>
		/// <value>The source column.</value>
		/// <remarks>Only required when using a Dataset as the source</remarks>
		[ConfigurationProperty(columnProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "DataSetParameterDataColumnDescription")]
        [ResourceDisplayName(typeof(DesignResources), "DataSetParameterDataColumnDisplayName")]
		public string Column
		{
			get { return (string)base[columnProperty]; }
			set { base[columnProperty] = value; }
		}

		/// <summary>  
		/// Gets or sets the source version.
		/// </summary>
		/// <value>The source version.</value>
		/// <remarks>
		/// Only required when using a Dataset as the source.
		/// Versions are Current, Default, Original and Proposed.
		/// </remarks>
        [ConfigurationProperty(versionProperty, IsRequired = false)]
        [ResourceDescription(typeof(DesignResources), "DataSetParameterDataVersionDescription")]
        [ResourceDisplayName(typeof(DesignResources), "DataSetParameterDataVersionDisplayName")]
		public DataRowVersion Version
		{
			get { return (DataRowVersion)base[versionProperty]; }
			set { base[versionProperty] = value; }
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameterData"/> class.
		/// </summary>
		public DataSetParameterData()
            : base(null, typeof(DataSetParameter))
		{
            this.Version = DataRowVersion.Current;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataSetParameterData"/> class.
		/// </summary>
		/// <param name="name">The parameter name.</param>
		/// <param name="dbType">Type of the parameter.</param>
		/// <param name="direction">The direction of the parameter relative to the DataSet.</param>
		/// <param name="column">The source column.</param>
		/// <param name="version">The source version.</param>
		public DataSetParameterData(string name, DbType dbType, ParameterDirection direction, string column, DataRowVersion version) 
            : base(name, typeof(DataSetParameter), dbType, direction)
		{
			this.Column = column;
			this.Version = version;
		}

        /// <summary>
        /// A <see cref="TypeRegistration"/> container configuration model for <see cref="DataSetParameterData"/>.
        /// </summary>
        /// <param name="name">The name to use when determining references to child elements.</param>
        /// <returns>A <see cref="TypeRegistration"/> for registering a <see cref="DataSetParameterData"/> in the container.</returns>
        public override IEnumerable<TypeRegistration> GetRegistrations(string name)
        {
            yield return
                new TypeRegistration<IParameter>(
                    () => new DataSetParameter(
                        Name, 
                        DbType, 
                        Direction, 
                        Column, 
                        Version))
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = name,
                    IsPublicName = false
                };
        }
	}
}
